package com.rabbit.work.listener;

import com.rabbit.work.po.Order;
import com.rabbit.work.service.IOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class CancelOrderListener {

    @Autowired
    private IOrderService orderService;

    @RabbitListener(queues = "qw.dlx")
    public void onMessage(@Payload String orderNo,
                          Channel channel,
                          @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) throws IOException {

        System.out.println("取消订单，订单号：" + orderNo);

        Order order = new Order();
        order.setOrderNo(orderNo);

        orderService.cancelOrder(order);

        channel.basicAck(deliveryTag, false);

    }
}
